//////////////////////////////
//HTTPService/REST handler.
//////////////////////////////
package org.bbdesign.airbuzz {
	import com.adobe.serialization.json.*;
	import flash.display.*;
	import flash.events.*;
	import mx.rpc.http.*;
	import mx.rpc.events.*;

	public class Httpserv extends Sprite  {
		//////////////////////////////
		//Internal class variables.
		//////////////////////////////
		//Instance.
		private var _httpservice:HTTPService = new HTTPService();
		private var _prop:Object;
		public var resultcontainer:Object = {};
		//////////////////////////////
		//Constructor.
		//////////////////////////////
		public function Httpserv(url:String, method:String, resultformat:String = 'object') {
			getHttpResp(url, method, resultformat);
		}
		//////////////////////////////

		//////////////////////////////

		//////////////////////////////
		//Private methods.
		//////////////////////////////

		//////////////////////////////

		//////////////////////////////
		//Get HTTP REST response.
		//////////////////////////////
		private function getHttpResp(url:String, method:String, resultformat:String):* {
			_httpservice.useProxy = false;
			_httpservice.url = url;
			_httpservice.method = method;
			//_httpservice.showBusyCursor = true;
			//_httpservice.destination = 'DefaultHTTPS';
			_httpservice.send();
			_httpservice.resultFormat = resultformat;
			//Events.
			_httpservice.addEventListener('result', resultHandler);
			_httpservice.addEventListener('fault', faultHandler);
		}
		//////////////////////////////

		//////////////////////////////
		//Event handlers.
		//////////////////////////////
		//Results.
		private function resultHandler(evt:ResultEvent):void {
			try {
				var result:String = evt.result as String;
				var decoder:JSONDecoder = new JSONDecoder(result, true);
				var resultobject:Object = (decoder.getValue() as Object);
				//Process results.
				procObject(resultobject);
			} catch(error:IOErrorEvent) {
				trace(error);
				dispatchEvent(new Event('HTTPSERV_OUTER_DATASENT_ERROR', true));
			} catch (error:JSONParseError) {
				trace(error);
				dispatchEvent(new Event('HTTPSERV_OUTER_DATASENT_ERROR_JSONPARSE_ERROR', true));
			}
		}
		//Faults.
		private function faultHandler(evt:FaultEvent):void {
			trace("Error: " + evt.message);
			dispatchEvent(new Event('HTTPSERV_OUTER_DATASENT_ERROR', true));
		}
		//////////////////////////////

		//////////////////////////////
		//Process object.
		//////////////////////////////
		private function procObject(obj:Object):void {
			for (var key:String in obj) {
				if(obj[key] != undefined || obj[key] != null) {
					//User data.
					resultcontainer.user_id = obj[key].id;
					resultcontainer.user_kind = obj[key].kind;
					resultcontainer.user_aboutMe = obj[key].aboutMe;
					resultcontainer.user_displayName = obj[key].displayName;
					resultcontainer.user_thumbnailUrl = obj[key].thumbnailUrl;
					resultcontainer.user_profileUrl = obj[key].profileUrl;
					//User activity data.
					resultcontainer.act_title = obj[key].title;
					resultcontainer.act_updated = obj[key].updated;
					resultcontainer.act_items = obj[key].items;
				}
			}
			dispatchEvent(new Event('HTTPSERV_OUTER_DATASENT_COMPLETE', true));
		}
	}
	//////////////////////////////
}

